package com.captain.special.数组;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/4/4 11:19
 */
public class 长度最小的子数组209 {
    public int minSubArrayLen(int target, int[] nums) {
        //
        //维系一个窗口
        int left = 0;
        int res = Integer.MAX_VALUE;
        //记录总和
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            //7   235243
            sum += nums[i];
            //当前总和 >= 目标值时, 找到子序列
            while (sum >= target) {
                res = Math.min(i - left + 1, res);
                sum = sum - nums[left];
                //left前移
                left++;
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}
